<?php

namespace App\Validate\Web;

use App\Validate\BaseValidate;
use App\Http\Models\ZtkUser;
use Illuminate\Support\Facades\Auth;

class WithdrawStoreValidate extends BaseValidate
{
    protected $rules = [];// 规则

    protected $message = [];// 自定义消息

    public function __construct($request)
    {
        parent::__construct($request);

        $this->rules = [
            'amount' => 'required|numeric',
        ];
        $this->message = [
            'amount.required' => '请输入提现金额',
            'amount.numeric' => '请输入正确的提现金额',
        ];
    }

    protected function customValidate()
    {
        $amount = $this->requestData['amount'];//提现金额
        if($amount < 100){
            $poundage = config('web.withdraw_min_poundage');
        }else{
            $poundage = number_format ($amount * config('web.withdraw_rate'),2);//提现手续费
        }

        $user = ZtkUser::select('id','amount','alipay_account','alipay_name')->find(Auth::id());
        if(!$user->alipay_account || !$user->alipay_name){
            $this->validator->errors()->add('alipay_account', '请先完善提现支付宝信息');

            return false;
        }

        if($amount > $user->amount){
            $this->validator->errors()->add('amount', '用户余额不足,当前余额：'.$user->amount);

            return false;
        }

        if($amount <= config('web.withdraw_min_poundage')){
            $this->validator->errors()->add('amount', '最低提现不能小于'.config('web.withdraw_min_poundage').'元');

            return false;
        }

        $this->requestData['poundage'] = $poundage;

    }
}
